package com.frx.datastruct.chapternine;

/**
 * @author ：frx
 * @date ：Created in 2019/3/7 22:38
 * @description：  线段树
 * @modified By：
 * @version: ToDO
 */
public class SegementTree<E> {

    private E[] tree;
    private E[] data;
    private Merger<E> merger;
    public SegementTree(E[] arr,Merger<E> merger){

        this.merger = merger;
        data = (E[])new Object[arr.length];
        for (int i = 0; i < arr.length; i++){
            data[i] = arr[i];
        }

        tree = (E[]) new Object[4 * arr.length];
        buildSegementTree(0,0,arr.length -1);
    }

    private void buildSegementTree(int treeIndex,int l,int r){

        if (l == r){
            tree[treeIndex] = data[l];
            return;
        }
        int leftIndex = leftChild(treeIndex);
        int rightIndex = rightchild(treeIndex);

        int mid = l + (r - l) / 2 ;
        buildSegementTree(leftIndex,l,mid);
        buildSegementTree(rightIndex,mid+1,r);

        tree[treeIndex] = merger.merge(tree[leftIndex],tree[rightIndex]);

    }


    private int leftChild(int index){
        return 2 * index + 1;
    }

    private int rightchild(int index){
        return 2 * index + 2;
    }

    public int getSize(){
        return data.length;
    }

    public E get(int index){
        if (index < 0 || index >= data.length){
            throw new IllegalArgumentException("Index is illegal !");
        }
        return data[index];
    }

    /** 线段树的查询方法  返回区间 [queryL...queryR] 的值 */
    public E query(int queryL,int queryR){
        if (queryL < 0 || queryR>= data.length ||
                queryR < 0 ||
                queryL >= data.length){
            throw new IllegalArgumentException("Index is Illegal !");
        }



        return query(0,0,data.length - 1,queryL,queryR);
    }
    /** 在以 treeIndex 为根的线段树中[l...r]的范围内，搜索区间[queryL...queryR]的值*/
    public E query(int treeIndex,int l,int r, int queryL,int queryR){

        // 递归中止的条件
        if (l == queryL && r == queryR){
            return tree[treeIndex];
        }

        int mid = l + (r - l)/2;

        int leftIndex = leftChild(treeIndex);
        int rightIndex = rightchild(treeIndex);

        if (queryL >= mid + 1){
            return query(rightIndex,mid + 1,r,queryL,queryR);
        } else if (queryR < mid){
            return query(leftIndex,l,mid,queryL,queryR);
        }
        E leftResult = query(leftIndex,l,mid,queryL,mid);
        E rightResult = query(rightIndex,mid+1,r,mid+1,queryR);
        return merger.merge(leftResult,rightResult);
    }




    @Override
    public String toString(){
        StringBuilder res = new StringBuilder();
        res.append('[');
        for (int i = 0; i < tree.length; i++){
            if (tree[i] != null){
                res.append(tree[i]);
            } else {
                res.append("null");
            }
            if ( i != tree.length - 1){
                res.append(", ");
            }
        }
        res.append(']');
        return res.toString();
    }
}
